﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{

    class _Nuttaps3
    {
        public static void Nuttaps3(Dictionary<string, Object> data)//螺母丝锥
{
    DxfDocument dxf = new DxfDocument();
    //图层
    Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
    Layer dimensioning = new Layer("dimensioning");    //尺寸层
    Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
    Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中性线
    Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
    Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
    Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
    Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
    Layer nocutting = new Layer("nocutting");    //非切削层
    Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
    Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
    Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
    Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
    Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
    Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
    Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
    Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层
                                                                   //作图
                                                                   //参数
    double pi = Math.PI;
    string path = Convert.ToString(data["path"]);
    double td = Convert.ToDouble(data["td"]);
    double tp = Convert.ToDouble(data["tp"]);
    double oal = Convert.ToDouble(data["oal"]);
    double thl = Convert.ToDouble(data["thl"]);
    double tcl = Convert.ToDouble(data["tcl"]);
    double dmm = Convert.ToDouble(data["dconms"]);
    double angular = Convert.ToDouble(data["angular"]);
    double waidingjian = Convert.ToDouble(data["waidingjian"]);
    double drvsc = Convert.ToDouble(data["drvsc"]);
    double drvl = Convert.ToDouble(data["drvl"]);

    /*double td = 20;
    double tp = 1.5;
    double oal = 220;
    double thl = 50;
    double tcl = 24;
    double dmm = 16;
    double angular = 2;
    double waidingjian = 0;
    double drvsc = 16;
    double drvl = 20;*/





    Vector2 v0 = new Vector2(0, 0);
    Vector2 v1 = fun.Polar(v0, 90 * pi / 180, td / 2);
    Vector2 v2 = fun.Polar(v0, pi, tcl);
    Vector2 v3 = fun.Polar(v0, 90 * pi / 180, dmm / 4);
    Vector2 v4 = fun.Inters(v1, fun.Polar(v1, (180 + angular) * pi / 180, 1), v2, fun.Polar(v2, 90 * pi / 180, 1));
    Vector2 v5 = new Vector2(v2.X, v3.Y);
    Vector2 v7 = fun.Inters(v4, fun.Polar(v4, -145 * pi / 180, 1), v2, v0);
    Vector2 v11 = fun.Polar(v5, 0, thl);
    Vector2 v8 = fun.Inters(v4, v1, v11, fun.Polar(v11, 90 * pi / 180, 1));
    Vector2 v16 = new Vector2(v2.X + oal, v0.Y + dmm / 2);
    Vector2 v10 = fun.Inters(v8, fun.Polar(v8, -45 * pi / 180, 1), v16, fun.Polar(v16, pi, 1));
    Vector2 v9 = fun.Polar(v8, -45 * pi / 180, fun.CalDistance(v8, v10) / 5 * 3);
    Vector2 v12 = new Vector2(v10.X, v3.Y);
    Vector2 v13 = fun.Inters(v1, v4, v9, fun.Polar(v9, pi, 1));
    Vector2 v14 = fun.Polar(v12, 0, (v8.Y - v0.Y));
    Vector2 v15 = new Vector2(v14.X, v16.Y);
    Vector2 v17 = fun.Inters(v16, fun.Polar(v16, -35 * pi / 180, 1), v2, v0);
    Vector2 a1 = new Vector2(v1.X, -v1.Y);
    Vector2 a2 = new Vector2(v2.X, -v2.Y);
    Vector2 a3 = new Vector2(v3.X, -v3.Y);
    Vector2 a4 = new Vector2(v4.X, -v4.Y);
    Vector2 a5 = new Vector2(v5.X, -v5.Y);
    Vector2 a8 = new Vector2(v8.X, -v8.Y);
    Vector2 a9 = new Vector2(v9.X, -v9.Y);
    Vector2 a10 = new Vector2(v10.X, -v10.Y);
    Vector2 a11 = new Vector2(v11.X, -v11.Y);
    Vector2 a12 = new Vector2(v12.X, -v12.Y);
    Vector2 a13 = new Vector2(v13.X, -v13.Y);
    Vector2 a14 = new Vector2(v14.X, -v14.Y);
    Vector2 a15 = new Vector2(v15.X, -v15.Y);
    Vector2 a16 = new Vector2(v16.X, -v16.Y);

    string tctr = Convert.ToString(data["tctr"]);
    //string tctr = "0";
    if (!tctr.Equals("0"))
    {
        Vector2 a01 = fun.Polar(v4, fun.CalAngle(v4, v8), fun.CalDistance(v4, v8) / 2);
        Vector2 a02 = fun.Polar(a01, 45 * pi / 180, 15);
        Vector2 a03 = fun.Polar(a01, 45 * pi / 180, 16);
        Line line101 = new Line(a01, a02);
        dxf.AddEntity(line101);
        fun.text(dxf, a03, 2, "丝锥公差带:" + tctr + "");

    }

    LwPolyline poly00 = new LwPolyline();
    poly00.Vertexes.Add(new LwPolylineVertex(v4));
    poly00.Vertexes.Add(new LwPolylineVertex(v8));
    poly00.Vertexes.Add(new LwPolylineVertex(v10));
    poly00.Vertexes.Add(new LwPolylineVertex(v16));
    fun.PolyFill(dxf, poly00, 0.3, contour);
    LwPolyline poly1 = new LwPolyline();
    poly1.Vertexes.Add(new LwPolylineVertex(a16));
    poly1.Vertexes.Add(new LwPolylineVertex(a10));
    poly1.Vertexes.Add(new LwPolylineVertex(a8));
    poly1.Vertexes.Add(new LwPolylineVertex(a4));
    fun.PolyFill(dxf, poly1, 0.3, contour);
    LwPolyline poly2 = new LwPolyline();
    poly2.Vertexes.Add(new LwPolylineVertex(v5));
    poly2.Vertexes.Add(new LwPolylineVertex(v14));
    poly2.Vertexes.Add(new LwPolylineVertex(v15));
    poly2.Vertexes[1].Bulge = -Math.Tan(-150 * pi / 180 / 4);
    fun.PolyFill(dxf, poly2, 0.3, contour);
    LwPolyline poly3 = new LwPolyline();
    poly3.Vertexes.Add(new LwPolylineVertex(a5));
    poly3.Vertexes.Add(new LwPolylineVertex(a14));
    poly3.Vertexes.Add(new LwPolylineVertex(a15));
    poly3.Vertexes[1].Bulge = -Math.Tan(150 * pi / 180 / 4);
    fun.PolyFill(dxf, poly3, 0.3, contour);
    Vector2[] Line4 = { v3, a3 };
    fun.drawBLine(dxf, Line4, 0.3, contour);
    Vector2[] Line5 = { v11, a11 };
    fun.drawBLine(dxf, Line5, 0.3, contour);
    Vector2[] Line6 = { v12, a12 };
    fun.drawBLine(dxf, Line6, 0.3, contour);
    Line line7 = new Line(v9, v13);
    line7.Lineweight = Lineweight.W25;
    line7.Layer = additionalline;
    dxf.AddEntity(line7);
    Line line8 = new Line(a9, a13);
    line8.Lineweight = Lineweight.W25;
    line8.Layer = additionalline;
    dxf.AddEntity(line8);

    //标注样式
    //箭头样式
    DimensionStyle dimensionStyle = fun.CreateDimStyle();
    dimensionStyle.ArrowSize = 1;
    dimensionStyle.ExtLineExtend = 0.6;//标注延长线长度/2
    dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
    dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
    //文本样式
    dimensionStyle.TextHeight = 1;
    double offset = 20;

    LinearDimension dim1 = new LinearDimension(new Line(v1, a1), thl + 10, 90, dimensionStyle);
    dim1.UserText = "TD = " + td;
    dxf.AddEntity(dim1);
    LinearDimension dim2 = new LinearDimension(new Line(a4, a3), offset * 0.5, 180, dimensionStyle);
    dim2.UserText = "TCL = " + tcl;
    dxf.AddEntity(dim2);

    double toh = Convert.ToDouble(data["thltctrh"]);
    double tol = Convert.ToDouble(data["thltctrl"]);
    //double toh = 0;
    //double tol = 0;
    if (toh == 0 && tol == 0)
    {
        LinearDimension dim3 = new LinearDimension(new Line(a4, a11), offset, 180, dimensionStyle);
        dim3.UserText = "THL = " + thl;
        dxf.AddEntity(dim3);
    }
    else
    {
        fun.dimrot(dxf, a4, a11, 0, -offset, 1, 2, -1, "THL = " + thl, toh + "", tol + "");
    }
    double toh1 = Convert.ToDouble(data["oaltctrh"]);
    double tol1 = Convert.ToDouble(data["oaltctrl"]);
    string oaltctr = Convert.ToString(data["oaltctr"]);
    /*double toh1 = 0;
    double tol1 =0;
    string oaltctr = "0";*/
    if (toh1 == 0 && tol1 == 0)
    {
        if (oaltctr.Equals("0"))
        {

            LinearDimension dim4 = new LinearDimension(new Line(a4, a16), offset * 1.5, 180, dimensionStyle);
            dim4.UserText = "OAL = " + oal;
            dxf.AddEntity(dim4);
        }
        else
        {
            LinearDimension dim4 = new LinearDimension(new Line(a4, a16), offset * 1.5, 180, dimensionStyle);
            dim4.UserText = "OAL = " + oal + "  " + oaltctr;
            dxf.AddEntity(dim4);
        }

    }
    else
    {
        fun.dimrot(dxf, a4, a16, 0, -offset * 1.5, 1, 2, -1, "OAL = " + oal, toh1 + "", tol1 + "");
    }

    string tcdconws = Convert.ToString(data["tcdconws"]);
    //string tcdconws = "0";
    if (tcdconws.Equals("0"))
    {
        LinearDimension dim5 = new LinearDimension(new Line(a16, v16), offset * 0.5, 90, dimensionStyle);
        dim5.UserText = "DCONMS = " + dmm;
        dxf.AddEntity(dim5);
    }
    else
    {
        LinearDimension dim5 = new LinearDimension(new Line(a16, v16), offset * 0.5, 90, dimensionStyle);
        dim5.UserText = "DCONMS = " + dmm + "  " + tcdconws;
        dxf.AddEntity(dim5);
    }





    if (td <= 10 && waidingjian == 1 && drvsc == 0)
    {

        Vector2 v18 = fun.Polar(v7, pi, dmm / 2);
        Vector2 v19 = fun.Polar(v17, 0, dmm / 2);

        LwPolyline poly7 = new LwPolyline();
        poly7.Vertexes.Add(new LwPolylineVertex(v4));
        poly7.Vertexes.Add(new LwPolylineVertex(v7));
        poly7.Vertexes.Add(new LwPolylineVertex(a4));
        poly7.Vertexes.Add(new LwPolylineVertex(a5));
        poly7.Vertexes[2].Bulge = -Math.Tan(-50 * pi / 180 / 4);
        poly7.Vertexes.Add(new LwPolylineVertex(v5));
        poly7.Vertexes.Add(new LwPolylineVertex(v4));
        poly7.Vertexes[4].Bulge = -Math.Tan(-50 * pi / 180 / 4);
        fun.PolyFill(dxf, poly7, 0.3, contour);

        LwPolyline poly8 = new LwPolyline();
        poly8.Vertexes.Add(new LwPolylineVertex(v16));
        poly8.Vertexes.Add(new LwPolylineVertex(v17));
        poly8.Vertexes.Add(new LwPolylineVertex(a16));
        poly8.Vertexes.Add(new LwPolylineVertex(v16));
        fun.PolyFill(dxf, poly8, 0.3, contour);


        Vector2[] Line9 = { v19, v18 };
        fun.drawPlinetype(dxf, Line9, 0.15, centre, Linetype.Center);

    }
    if (drvsc != 0)
    {
        Vector2 v18 = fun.Polar(v2, pi, dmm / 2);
        Vector2 v19 = fun.Polar(new Vector2(v16.X, v0.Y), 0, dmm / 2);
        Vector2 v20 = fun.Polar(v16, -90 * pi / 180, dmm / 3);
        Vector2 v21 = fun.Polar(v20, pi, drvl);
        Vector2 v22 = new Vector2(v21.X, v16.Y);
        Vector2 a20 = new Vector2(v20.X, -v20.Y);
        Vector2 a21 = new Vector2(v21.X, -v21.Y);
        Vector2 a22 = new Vector2(v22.X, -v22.Y);


        Vector2 v019 = fun.Polar(v19, 0, drvsc * 3);
        Vector2 v020 = fun.Polar(v019, 90 * pi / 180, drvsc);
        Vector2 v021 = fun.Polar(v019, 0, drvsc);
        Vector2 v022 = fun.Polar(v019, pi, drvsc);
        Vector2 v023 = fun.Polar(v019, -90 * pi / 180, drvsc);
        Vector2 v020p = fun.Polar(v019, 90 * pi / 180, drvsc * Math.Sin(45 * pi / 180));
        Vector2 v021p = fun.Polar(v019, 0, drvsc * Math.Sin(45 * pi / 180));
        Vector2 v022p = fun.Polar(v019, pi, drvsc * Math.Sin(45 * pi / 180));
        Vector2 v023p = fun.Polar(v019, -90 * pi / 180, drvsc * Math.Sin(45 * pi / 180));
        Vector2 v024 = fun.Polar(v020p, -135 * pi / 180, drvsc / 5);
        Vector2 v025 = fun.Polar(v020p, -45 * pi / 180, drvsc / 5);
        Vector2 v026 = fun.Polar(v021p, 135 * pi / 180, drvsc / 5);
        Vector2 v027 = fun.Polar(v021p, -135 * pi / 180, drvsc / 5);
        Vector2 v028 = fun.Polar(v023p, 45 * pi / 180, drvsc / 5);
        Vector2 v029 = fun.Polar(v023p, 135 * pi / 180, drvsc / 5);
        Vector2 v030 = fun.Polar(v022p, -45 * pi / 180, drvsc / 5);
        Vector2 v031 = fun.Polar(v022p, 45 * pi / 180, drvsc / 5);


        LwPolyline poly7 = new LwPolyline();
        poly7.Vertexes.Add(new LwPolylineVertex(v4));
        poly7.Vertexes.Add(new LwPolylineVertex(a4));
        fun.PolyFill(dxf, poly7, 0.3, contour);

        Vector2[] Line9 = { v16, a16 };
        fun.drawBLine(dxf, Line9, 0.3, contour);
        Vector2[] Line10 = { v20, v21 };
        fun.drawBLine(dxf, Line10, 0.3, contour);
        Vector2[] Line11 = { v21, v22 };
        fun.drawBLine(dxf, Line11, 0.3, contour);
        Vector2[] Line12 = { a20, a21 };
        fun.drawBLine(dxf, Line12, 0.3, contour);
        Vector2[] Line13 = { a21, a22 };
        fun.drawBLine(dxf, Line13, 0.3, contour);
        Vector2[] Line14 = { v19, v18 };
        fun.drawPlinetype(dxf, Line14, 0.15, centre, Linetype.Center);
        Vector2[] Line15 = { v020, v023 };
        fun.drawPlinetype(dxf, Line15, 0.15, centre, Linetype.Center);
        Vector2[] Line16 = { v022, v021 };
        fun.drawPlinetype(dxf, Line16, 0.15, centre, Linetype.Center);

        LwPolyline poly8 = new LwPolyline();
        poly8.Vertexes.Add(new LwPolylineVertex(v031));
        poly8.Vertexes.Add(new LwPolylineVertex(v024));
        poly8.Vertexes.Add(new LwPolylineVertex(v025));
        poly8.Vertexes[1].Bulge = -Math.Tan(pi / 8);
        poly8.Vertexes.Add(new LwPolylineVertex(v026));
        poly8.Vertexes[3].Bulge = -Math.Tan(pi / 8);
        poly8.Vertexes.Add(new LwPolylineVertex(v027));
        poly8.Vertexes.Add(new LwPolylineVertex(v028));
        poly8.Vertexes[5].Bulge = -Math.Tan(pi / 8);
        poly8.Vertexes.Add(new LwPolylineVertex(v029));
        poly8.Vertexes.Add(new LwPolylineVertex(v030));
        poly8.Vertexes[7].Bulge = -Math.Tan(pi / 8);
        poly8.IsClosed = true;
        fun.PolyFill(dxf, poly8, 0.3, contour);

        LwPolyline poly9 = new LwPolyline();
        String[] htype1 = new String[100];
        Line[] hline1 = new Line[100];
        Arc[] harc1 = new Arc[100];
        int hnumst1 = poly8.Explode().Count;  //记录爆炸后，线段有多少
        for (int i = 0; i < hnumst1; i++)
        {
            htype1[i] = poly8.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组
            if (htype1[i] == "Line")
            {
                hline1[i] = (Line)poly8.Explode()[i];
            }
            if (htype1[i] == "Arc")
            {
                harc1[i] = (Arc)poly8.Explode()[i];
            }
        }
        fun.hatchsub1(dxf, htype1, hnumst1, 45, 5, hline1, harc1, hatching);


        LinearDimension dim10 = new LinearDimension(new Line(v029, v026), offset, -135, dimensionStyle);
        dim10.UserText = "DRVSC = " + drvsc;
        dxf.AddEntity(dim10);

        LinearDimension dim11 = new LinearDimension(new Line(a20, a21), offset, 180, dimensionStyle);
        dim11.UserText = "DRVL = " + drvl;
        dxf.AddEntity(dim11);


    }
    else
    {

        Vector2 v18 = fun.Polar(v2, pi, dmm / 2);
        Vector2 v19 = fun.Polar(new Vector2(v16.X, v0.Y), 0, dmm / 2);
        Vector2[] Line9 = { v4, a4 };
        fun.drawBLine(dxf, Line9, 0.3, contour);
        Vector2[] Line10 = { v16, a16 };
        fun.drawBLine(dxf, Line10, 0.3, contour);
        Vector2[] Line14 = { v19, v18 };
        fun.drawPlinetype(dxf, Line14, 0.15, centre, Linetype.Center);

    }

            double isframe = Convert.ToInt32(data["isframe"]);
            String isframe_chose = Convert.ToString(data["isframe_chose"]);
            double scale = Convert.ToDouble(data["scale"]); //放大系数
            Vector2 p0 = new Vector2(0, 0);
            if (isframe == 1)
            {
                if (isframe_chose.Equals("0"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA0X(dxf, new Vector2(p0.X - 1189 / 2.0, p0.Y + 841 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("1"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA1X(dxf, new Vector2(p0.X - 841 / 2.0, p0.Y + 594 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("2"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA2X(dxf, new Vector2(p0.X - 594 / 2.0, p0.Y + 420 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("3"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA3X(dxf, new Vector2(p0.X - 420 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4X"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4X(dxf, new Vector2(p0.X - 297 / 2.0, p0.Y + 210 / 2.0 - 10 * 2.26), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }

                if (isframe_chose.Equals("4Y"))
                {
                    Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 210 / 2.0, p0.Y + 297 / 2.0), data);
                    fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
                }
            }




            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
}
}
